package edu.mis.modules.product;
import com.jfinal.core.Controller;

import edu.mis.model.Product;
import edu.mis.model.Type;

public class ProductController extends Controller{

	static ProductService productService=new ProductService();
	
	public void index() {
		setAttr("typeList",Type.dao.findAll());
		render("product.html");
	}
	
	public void list(){
		setAttr("data", productService.findAllProductWithType());
		renderJson();   
	}
	
	public void getType() {
		setAttr("typeList",Type.dao.findAll());
		renderJson();
	}
	
	public void save(){
		Product product  =  getModel(Product.class);
		if(productService.save(product)){
			setAttr("result", true);
			setAttr("msg", "添加商品成功!");
		}else{
			setAttr("result",false);
		}
		renderJson();
	}
	
	public void edit() {
		setAttr("product", productService.findProductById(getParaToInt("id")));
		renderJson();
	}
	
	public void update(){
		Product product  =  getModel(Product.class);
		if(productService.update(product)){
			setAttr("result", true);
			setAttr("msg", "修改商品成功!");
		}else{
			setAttr("result", false);
		}
		renderJson();
	}
	
	public void delete() {
		if(productService.deleteProductById(getParaToInt("id"))){
			setAttr("result", true);
			setAttr("msg", "删除商品成功!");
		}else{
			setAttr("result", false);
		}
		renderJson();
	}
}